﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections;
using System.Diagnostics;
using WebCoreAPI.Models;

namespace WebCoreAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        [HttpPost]
        public ActionResult<LoginResult> Login(LoginRequest loginReq) {
            if (loginReq.UseName == "admin" && loginReq.Password == "123456")
            {
                var processes = Process.GetProcesses().Select(p => new ProcessInfo(
                    p.Id, p.ProcessName, p.WorkingSet64)).ToArray();
                return new LoginResult(true, processes);
            }
            else
            {
                return new LoginResult(false, null);
            }
        }

        /// <summary>
        /// 获取环境变量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IDictionary GetEnvironmentVar() {
            return Environment.GetEnvironmentVariables();
        }

        /// <summary>
        /// 获取自定义的环境变量
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [SelfTransaction]
        public String? GetSelfEnvironmentVar() 
        {
            //点击项目的属性》常规》打开调试启动配置UI,就可以配置自己的环境变量
            return Environment.GetEnvironmentVariable("feng");
        }

        /// <summary>
        /// 有异常的Action
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public String? ExceptionMethod() 
        {
          return System.IO.File.ReadAllText("c:/1.txt");
        }
    }
}
